package com.fairytask.engine.components.button
{
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.utils.draw.DrawAPI;
	import com.fairytask.managers.ResManager;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	
	/**
	 * 普通按钮 
	 * @author Hanjy
	 * 
	 */
	public class Button extends BaseComponent
	{
		/**按钮皮肤区域**/
		private var _btnSkinSpace:Sprite;
		
		/**按钮label区域**/
		private var _labelSpace:Sprite;
		
		/**按钮图标容器*/
		private var _btnIconSpace:Sprite;
		
		/**热区**/
		private var _hotAreaSpace:Sprite;
		
		/**标签显示**/
		private var _icon:Clip;
		
		/**标题颜色**/
		private var _labelColor:uint = 0xFFFFFF;
		
		/**高**/
		private var _width:int;
		
		/**宽**/
		private var _height:int;
		
		/**按钮label上偏移量**/
		private var _top:int	=	2;
		
		/**按钮一般状态*/
		private var _buttonNormal:MovieClip;
		
		/**鼠标经过状态*/
		private var _buttonOver:MovieClip;
		
		/**鼠标按下状态*/
		private var _buttonDown:MovieClip;
		
		/**禁用状态*/
		private var _buttonUnallowed:MovieClip;
		
		/**按钮当前状态**/
		private var _currentState:String = ButtonState.NORMAL;
		
		/**是否活动**/
		private var _enabled:Boolean = true;
		
		/**按钮的文本颜色,描边色,描边大小*/
		private var btnTextLineColor :uint	= 0XFFFF00;
		
		/**效果大小**/
		private var _btnTextLineSize :int =	0;
		
		private var _data					:Object;
		
		/**
		 * 
		 * @param label 按钮文字
		 * @param width 宽度
		 * @param height 高度
		 * @param skin 皮肤
		 * @param top 上偏移量
		 * @param icon icon显示对象/url
		 * @param iconX x坐标
		 * @param iconY y坐标
		 * 
		 */
		public function Button(label:String = "",width:uint=80,height:uint=27,skin:String = null,top:int = 0,icon:* = "",iconX:int = 0,iconY:int = 0) 
		{
			this.buttonMode = true;
			_label	=	label;
			_width	=	width;
			_height	=	height;
//			//Button_0103按钮太多，默认文字居中调整
//			if(top==0&&skin=="Button_0103")
//			{
//				_top	=	_height/2-8;
//			}
//			else
//			{
			_top	=	top;
//			}
			//获得按钮状态mc
			if(skin != null){
				var mc:MovieClip = ResManager.newResClass(skin) as MovieClip;
				_buttonNormal = mc.getChildByName("Button_normal") as MovieClip;
				_buttonOver = mc.getChildByName("Button_over") as MovieClip;
				_buttonDown = mc.getChildByName("Button_down") as MovieClip;
				_buttonUnallowed = mc.getChildByName("Button_unallowed") as MovieClip;
				
				_buttonNormal.x	= _buttonOver.x	= _buttonDown.x	= _buttonUnallowed.x = 0;
				_buttonNormal.y	= _buttonOver.y	= _buttonDown.y	= _buttonUnallowed.y = 0;			
				_buttonNormal.width	= _buttonOver.width	= _buttonDown.width = _buttonUnallowed.width =	width;
				_buttonNormal.height = _buttonOver.height = _buttonDown.height = _buttonUnallowed.height =	height;
			}
			init(icon,iconX,iconY);
		}
		
		private function init(icon:*,iconX:int,iconY:int):void
		{
			//创建容器
			createStateSpace();
			createLabel();
			createIconSpace();
			createIcon(icon,iconX,iconY);
			createHotAreaSpace();
			addListener();
		}
		
		/**
		 * 设置皮肤 
		 * @param value
		 * 
		 */
		override public function set skin(value:String):void
		{
			if(value != null&&_skin!=value){
				var mc:MovieClip = ResManager.newResClass(value) as MovieClip;
				_buttonNormal = mc.getChildByName("Button_normal") as MovieClip;
				_buttonOver = mc.getChildByName("Button_over") as MovieClip;
				_buttonDown = mc.getChildByName("Button_down") as MovieClip;
				_buttonUnallowed = mc.getChildByName("Button_unallowed") as MovieClip;
				
				_buttonNormal.x	= _buttonOver.x	= _buttonDown.x	= _buttonUnallowed.x = 0;
				_buttonNormal.y	= _buttonOver.y	= _buttonDown.y	= _buttonUnallowed.y = 0;			
				_buttonNormal.width	= _buttonOver.width	= _buttonDown.width = _buttonUnallowed.width =	width;
				_buttonNormal.height = _buttonOver.height = _buttonDown.height = _buttonUnallowed.height =	height;
				
				updataBtnState();
			}
			_skin	=	value;
		}
		
		/**热区**/
		private function createHotAreaSpace():void
		{
			_hotAreaSpace = new Sprite();
			
			_hotAreaSpace.graphics.clear();
			_hotAreaSpace.graphics.beginFill(0xff0000,0);
			_hotAreaSpace.graphics.drawRect(0,0,_width,_height);
			_hotAreaSpace.graphics.endFill();
			
			this.addChild(_hotAreaSpace);
		}
		
		/**按钮状态区域**/
		private function createStateSpace():void
		{
			_btnSkinSpace = new Sprite();
			_btnSkinSpace.mouseChildren = _btnSkinSpace.mouseEnabled = false;
			this.addChild(_btnSkinSpace);
			updataBtnState();
		}
		
		private function createIconSpace():void
		{
			_btnIconSpace = new Sprite();
			_btnIconSpace.mouseChildren = _btnIconSpace.mouseEnabled = false;
			this.addChild(_btnIconSpace);
		}
		
		/**按钮label区域**/
		private function createLabel():void
		{
			_labelSpace = new Sprite();
			_labelSpace.mouseChildren = _labelSpace.mouseEnabled = false;
			this.addChild(_labelSpace);
		}
		
		/**画出按钮label**/
		private function drawLabel():void
		{
			clearAll(_labelSpace);
			
			//获得icon位置，技术txt位置
			var iconWidth:int = _icon == null ? 0 : _btnIconSpace.width + _btnIconSpace.x + 2; //2是icon和txt之间的距离
			
			/*var _txtLabel:TextField = new TextField();
			_txtLabel.htmlText = _label;
			_txtLabel.width = _width - iconWidth;
			_txtLabel.multiline = true;*/
			
			//不可活动的时候，灰掉
			var txtColor:uint = enabled ? _labelColor: 0x9f9f9f;
			//_txtLabel.textColor = txtColor;
			
			//_txtLabel.alwaysShowSelection = true;
			//没图标的时候居中 || 有图标，紧邻图标
			/*if(iconWidth == 0)
			{
				_txtLabel.autoSize = TextFieldAutoSize.CENTER;
			}*/
			//btnEffect(_txtLabel);
			
			var textBitMap:	Bitmap	=	DrawAPI.getStringBitmap(_label,{
				autoSize:TextFieldAutoSize.LEFT,
				textColor:txtColor,
				multiline:true,
				wordWrap:false,
				filters:[Filter.COMMON_LABEL_FILTER]
			});
			
			/*var textBitMapData:BitmapData = new BitmapData(_width,_height,true,0XFF8800);
			textBitMapData.draw(_txtLabel);
			var textBitMap:	Bitmap = new Bitmap(textBitMapData);*/
			if(_top == 0)
			{
				//textBitMap的高度比实际文字高度要低2像素，暂时这样处理
				textBitMap.y = (_height - (textBitMap.height + 2))/2;
			}else
			{
				textBitMap.y = _top;
			}
			//textBitMap的宽度比实际文字宽度要宽4像素，暂时这样处理
			textBitMap.x = iconWidth+((_width-iconWidth)-(textBitMap.width - 4))/2;
			_labelSpace.addChild(textBitMap);
		}
		
		/**
		 * 
		 * @param view 显示对象/url
		 * @param dx
		 * @param dy
		 * 
		 */
		public function createIcon(view:*,dx:int = 0 , dy:int = 0):void
		{
			if(_icon != null){
				_icon.clear();
				_icon = null;
				clearAll(_btnIconSpace);
			}
			if(view != null && view != "")
			{
				_icon = new Clip();
				_icon.source = view;
				_btnIconSpace.addChild(_icon);
				_btnIconSpace.x	= dx;
				_btnIconSpace.y	= dy;
			}
			drawLabel();
		}
		
		private function addListener():void
		{
			_hotAreaSpace.addEventListener(MouseEvent.CLICK,onClick);
			_hotAreaSpace.addEventListener(MouseEvent.ROLL_OUT,onOut);
			_hotAreaSpace.addEventListener(MouseEvent.ROLL_OVER,onUpHandle);
			_hotAreaSpace.addEventListener(MouseEvent.MOUSE_DOWN,onDownHandle);
			_hotAreaSpace.addEventListener(MouseEvent.MOUSE_UP,onUpHandle);
		}
		
		private function removeListener():void
		{
			_hotAreaSpace.removeEventListener(MouseEvent.CLICK,onClick);
			_hotAreaSpace.removeEventListener(MouseEvent.ROLL_OUT,onOut);
			_hotAreaSpace.removeEventListener(MouseEvent.ROLL_OVER,onUpHandle);
			_hotAreaSpace.removeEventListener(MouseEvent.MOUSE_DOWN,onDownHandle);
			_hotAreaSpace.removeEventListener(MouseEvent.MOUSE_UP,onUpHandle);
		}
		
		private function onClick(e:MouseEvent):void
		{
		}
		
		private function onOut(e:MouseEvent):void
		{
			_currentState = ButtonState.NORMAL;
			updataBtnState();
		}
		
		private function onRollOutHandle(e:MouseEvent):void
		{
			_currentState = ButtonState.OVER;
			updataBtnState();
		}
		
		private function onDownHandle(e:MouseEvent):void
		{
			_currentState = ButtonState.DOWN;
			updataBtnState();
		}
		
		private function onUpHandle(e:MouseEvent):void
		{
			_currentState = ButtonState.OVER;
			updataBtnState();
		}
		
		/**
		 * 清空
		 */
		private function clearAll(space:Sprite):void
		{
			if(space == null)return; 
			while(space.numChildren > 0){
				space.removeChildAt(0);	
			}
		}
		
		/**
		 * 更新按钮状态显示皮肤
		 */
		private function updataBtnState():void
		{
			clearAll(_btnSkinSpace);
			
			var stateSkin:MovieClip;
			switch(_currentState){
				case ButtonState.NORMAL:
					stateSkin = _buttonNormal;
					if(_labelSpace)
					{
						_labelSpace.y	= 0;
					}
					break;
				case ButtonState.OVER:
					stateSkin = _buttonOver;
					if(_labelSpace)
					{
						_labelSpace.y	= 0;
					}
					break;
				case ButtonState.DOWN:
					stateSkin = _buttonDown;
					if(_labelSpace)
					{
						_labelSpace.y	= 2;
					}
					break;
				case ButtonState.UNALLOWED:
					stateSkin = _buttonUnallowed;
					break;
			}
			_btnSkinSpace.addChild(stateSkin);
		}
		
		override public function get label():String
		{
			return _label;
		}
		
		/**按钮label**/
		override public function set label(value:String):void
		{
			_label = value;
			drawLabel();
		}
		
		/**设置label颜色**/
		public function set labelColor(color:uint):void
		{
			_labelColor = color;
			drawLabel();
		}
		
		/**按钮当前状态**/
		public function get currentState():String
		{
			return _currentState;
		}
		
		/**
		 * @private
		 */
		public function set currentState(value:String):void
		{
			_currentState = value;
			updataBtnState();
		}
		
		/**是否禁用**/
		override public function get enabled():Boolean
		{
			return _enabled;
		}
		
		/**
		 * @private
		 */
		override public function set enabled(value:Boolean):void
		{
			_enabled = value;
			if(! _enabled){
				currentState = ButtonState.UNALLOWED;
				removeListener();
				this.mouseEnabled = this.mouseChildren = false;
			}else{
				currentState = ButtonState.NORMAL;
				addListener();
				this.mouseEnabled = this.mouseChildren = true;
			}
			drawLabel();
		}
		
		/**
		 * 鼠标事件可用
		 * @param val
		 * 
		 */
		public function set btnCanMouse(val:Boolean):void
		{
			this.mouseEnabled = this.mouseChildren = val;
		}
		
		/**根据滤镜的大小确定是否做发光效果*/
		private function btnEffect(labelTxt:TextField):void
		{
			labelTxt.filters =	[Filter.COMMON_LABEL_FILTER];
		}
		
		/**效果大小**/
		public function get btnTextLineSize():int
		{
			return _btnTextLineSize;
		}
		
		/**
		 * @private
		 */
		public function set btnTextLineSize(value:int):void
		{
			_btnTextLineSize = value;
			drawLabel();
		}

		public function get data():Object
		{
			return _data;
		}

		public function set data(value:Object):void
		{
			_data = value;
		}

		public function set isSelect(val:Boolean):void
		{
			if(val)
			{
				removeListener();
				_currentState = ButtonState.DOWN;
				updataBtnState();
			}
			else
			{
				addListener();
				_currentState = ButtonState.NORMAL;
				updataBtnState();
			}
		}
	}
}